/**
 * Created by wanthering on 2017/8/16.
 */

const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.ObjectId

const StudentSchema = new mongoose.Schema({
  phone: {type:String},
  registered:{type:Boolean,default:false},
  lesson: [{
    _id:ObjectId,
    award:Number,
    answered:{type:Boolean,default:false},
    withdrew:{type:Boolean,default:false}
  }],
  lastAwardTime:Date,
  meta:{
    createAt:{type:Date,default:Date.now()},
    updateAt:{type:Date,default:Date.now()}
  }
});

StudentSchema.pre('save',function(next){
  if(this.isNew){
    this.meta.createAt = this.meta.updateAt = Date.now();
  }else{
    this.meta.updateAt = Date.now();
  }
  next();
});

module.exports = mongoose.model('Student',StudentSchema);